package dimension.starry.block.machine.industry.smallLathe;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.industry.IndustryMachineRegister;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.machine.simple.weaponTable.WeaponTableContainer;
import dimension.starry.block.typeInterface.MachineTileEntity;
import dimension.starry.item.ItemRegisterManager;
import dimension.starry.item.typeInterface.WeaponType.WeaponType;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class SmallLatheTileEntity extends MachineTileEntity {

    private static final Text TITLE = Text.translatable("block." + StarryDimension.MOD_ID + ".small_lathe");

    public SmallLatheTileEntity(BlockPos blockPos, BlockState blockState) {
        super(IndustryMachineRegister.SMALL_LATHE_LEFT.getBlockEntityType(), blockPos, blockState, 3, TITLE, 800);
    }

    private static Item getOutput(ItemStack input1, ItemStack input2) {
        Item item1 = input1.getItem();
        Item item2 = input2.getItem();
        if (ItemRegisterManager.STEEL.getItem().equals(item1) &&
            ItemRegisterManager.WATER_STONE_POT.getItem().equals(item2)) {
            return ItemRegisterManager.SCREW.getItem();
        } else if (ItemRegisterManager.STEEL.getItem().equals(item1) && Items.IRON_INGOT.equals(item2)) {
            return ItemRegisterManager.NUT.getItem();
        }
        return null;
    }

    @Override
    public ScreenHandler createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new SmallLatheContainer(
            IndustryMachineRegister.SMALL_LATHE_LEFT.getScreenHandlerType(), sycID, inventory, getPos());
    }

    @Override
    public void tick(World world, BlockPos pos, BlockState state1, MachineTileEntity blockEntity) {
        if (world == null || world.isClient()) {
            return;
        }
        ItemStack inputStack1 = this.inventory.getStack(0);
        ItemStack inputStack2 = this.inventory.getStack(1);

        if (inputStack1.isEmpty() || inputStack2.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getOutput(inputStack1, inputStack2) == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(getOutput(inputStack1, inputStack2));
            if (this.inventory.canInsert(itemStack)) {
                inputStack1.setCount(inputStack1.getCount() - 1);
                inputStack2.setCount(inputStack2.getCount() - 1);
                this.resetProgress();
                this.inventory.addStack(itemStack);
            }
        } else {
            this.addProgress();
        }
    }


}
